package cn.e4j.modular.crm.service.impl;

import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.core.util.InvokeUtil;
import cn.e4j.modular.crm.entity.GwgfT;
import cn.e4j.modular.crm.mapper.GwgfTMapper;
import cn.e4j.modular.crm.service.GwgfTService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author liuwei
 * @since 2019-11-22
 */
@Service
public class GwgfTServiceImpl extends ServiceImpl<GwgfTMapper, GwgfT> implements GwgfTService {

    @Override
    public List<Map<String, Object>> getHeadList(Page<Object> page, Dto inDto) {
        List<Map<String, Object>> gwgfTList = InvokeUtil.invokeBeanMethod("gwgfTMapper,getHeadList", page,inDto);

        if (gwgfTList != null) {
            gwgfTList.forEach(gwgf -> {
                List<Map<String,String>> gwgf007MapList = this.baseMapper.getGwGf007List((String)gwgf.get("GWGF001"));

                List<String> gwgf007List = new ArrayList<>();
                List<String> gwgf007NameList = new ArrayList<>();
                if(gwgf007MapList!=null && gwgf007MapList.size() > 0) {
                    gwgf007MapList.forEach(gwgf007Map -> {
                        if(gwgf007Map!=null && gwgf007Map.get("GWGF007") != null){
                            gwgf007List.add(gwgf007Map.get("GWGF007"));
                            gwgf007NameList.add(gwgf007Map.get("GWGF007_NAME"));
                        }
                    });
                }

                gwgf.put("GWGF007",StringUtils.join(gwgf007List,";"));
                gwgf.put("GWGF007_NAME",StringUtils.join(gwgf007NameList,";"));
            });
        }
        return gwgfTList;
    }

    @Override
    public boolean existedTheParams(GwgfT gwgfT) {
        ShiroUser shiroUser = ShiroKit.getUser();
        QueryWrapper<GwgfT> gwgfWrapper = new QueryWrapper<>();
        gwgfWrapper.eq("GWGF001",gwgfT.getGWGF001());
        gwgfWrapper.eq("GWGFENT",shiroUser.getDBC());
        return this.baseMapper.selectList(gwgfWrapper).size() > 0;
    }

    @Override
    @Transactional
    public void add(GwgfT gwgfT) {
        ShiroUser shiroUser = ShiroKit.getUser();
        String gwgf007_str = gwgfT.getGWGF007();
        gwgfT.setGWGFENT(shiroUser.getDBC());
        if(StringUtils.isNotEmpty(gwgf007_str)){
            List<String> gwgf007List = Arrays.asList(gwgf007_str.split(";"));
            gwgf007List.forEach(str -> {
                gwgfT.setGWGF007(str);
                gwgfT.insert();
            });
        }else {
            gwgfT.insert();
        }

    }

    @Override
    @Transactional
    public void edit(GwgfT gwgfT) {
        ShiroUser shiroUser = ShiroKit.getUser();
        UpdateWrapper<GwgfT> gwgfWrapper = new UpdateWrapper<>();
        gwgfWrapper.eq("GWGF001",gwgfT.getGWGF001());
        gwgfWrapper.eq("GWGFENT",shiroUser.getDBC());
        gwgfT.delete(gwgfWrapper);

        this.add(gwgfT);
    }

    @Override
    @Transactional
    public void delete(GwgfT gwgfT) {
        ShiroUser shiroUser = ShiroKit.getUser();
        UpdateWrapper<GwgfT> gwgfWrapper = new UpdateWrapper<>();
        gwgfWrapper.eq("GWGF001",gwgfT.getGWGF001());
        gwgfWrapper.eq("GWGFENT",shiroUser.getDBC());
        gwgfT.delete(gwgfWrapper);
    }

    @Override
    @Transactional
    public void deleteBatch(List<GwgfT> gwgfTList) {
        gwgfTList.forEach(this::delete);
    }

    @Override
    @Transactional
    public void changeStatusBatch(List<GwgfT> gwgfTList) {
        ShiroUser shiroUser = ShiroKit.getUser();
        gwgfTList.forEach(gwgfT -> {
            GwgfT updateGwgfT = new GwgfT();
            updateGwgfT.setGWGFACTI(gwgfT.getGWGFACTI());

            UpdateWrapper<GwgfT> gwgfTUpdateWrapper = new UpdateWrapper<>();
            gwgfTUpdateWrapper.eq("GWGF001", gwgfT.getGWGF001());
            gwgfTUpdateWrapper.eq("GWGFENT", shiroUser.getDBC());

            updateGwgfT.update(gwgfTUpdateWrapper);
        });
    }
}
